from django import forms
from .models import *
from django.db.models import Q
from urllib.parse import parse_qs, urlparse
from apps.repo.models import Repo
from django.forms.fields import TextInput

class IndexBannerForm(forms.ModelForm):
    class Meta:
        firelds = ('link', )

    link = forms.CharField(required=False, label='链接')
    banner_public_file_id = forms.CharField(required=True, label='Banner', widget=TextInput(attrs={'class': 'upload'}))

class StoreForm(forms.ModelForm):
    class Meta:
        model = Store
        fields = ('name', 'store_type', 'video_banner_public_file_id')

    video_banner_public_file_id = forms.CharField(label="视频介绍", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id0 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id1 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id2 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id3 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id4 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))



class BrandForm(forms.ModelForm):
    class Meta:
        model = Brand
        fields = ('store', 'name', 'logo_public_file_id', 'video_banner_public_file_id', 'no')

    logo_public_file_id = forms.CharField(label='LOGO图片', required=True, widget=TextInput(attrs={'class': 'upload'}))
    video_banner_public_file_id = forms.CharField(label="视频介绍", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id0 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id1 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id2 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id3 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id4 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))

    def clean(self):
        no = self.cleaned_data.get('no')
        if Brand.objects.filter(no=no):
            raise forms.ValidationError('品牌编号重复或没变化')
        if no and len(no) != 3:
            raise forms.ValidationError('品牌编号必须是3位')

class PackageForm(forms.ModelForm):
    class Meta:
        model = Package
        fields = ('name', 'price', 'avatar_public_file_id')

    avatar_public_file_id = forms.CharField(label='logo', required=True, widget=TextInput(attrs={'class': 'upload'}))

class ProductForm(forms.ModelForm):
    """
    #
    """
    class Meta:
        model = Product
        fields = ('brand', 'name', 'tea_type',  'no',
                  'age_code', 'tea_cake_type',  'pre_sale_price', 'pre_sale_weixin_article_link',
                  'pre_sale_start_time',  'pre_sale_end_time',
                  'specification', 'kind', 'material', 'age', 'publish_count', 'publish_unit', 'technology', 'tea_mountain', 'altitude',
                  'ancient_tea_tree_count', 'year_avg_temperature', 'year_avg_precipitation', 'made_in','logo_public_file_id', 'video_banner_public_file_id',
                  'status', 'free_repo_cost',)

    logo_public_file_id = forms.CharField(required=True, label='Logo', widget=TextInput(attrs={'class': 'upload'}))
    video_banner_public_file_id = forms.CharField(required=False, label='视频Banner', widget=TextInput(attrs={'class': 'upload'}))
    repo_id = forms.ChoiceField(choices=Repo.make_choices())
    packages = forms.MultipleChoiceField(choices=Package.make_choices(),required=False)
    banner_id0 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id1 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id2 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id3 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))
    banner_id4 = forms.CharField(label="长图", required=False, widget=TextInput(attrs={'class': 'upload'}))

    def clean(self):
        no = self.cleaned_data.get('no')
        if no and len(no) != 3:
            raise forms.ValidationError('产品编号必须是3位')
        #
        age_code = self.cleaned_data.get('age_code')
        if age_code and len(age_code) != 5:
            raise forms.ValidationError('年份码必须是5位')
        #
        start_time = self.cleaned_data.get('pre_sale_start_time')
        end_time = self.cleaned_data.get('pre_sale_end_time')
        if start_time and end_time and start_time >= end_time:
            raise forms.ValidationError('结束时间必须大于开始时间')

class GoodsBlockForm(forms.ModelForm):
    class Meta:
        model = GoodsBlock
        fields = ('unique_code',)

    def clean(self):
        print(self.data)
        unique_code_url = self.data['unique_code']
        rs = urlparse(unique_code_url)
        params = parse_qs(rs.query, True)
        unique_code_param = params.get('unique_code', None)
        unique_code = unique_code_param[0] if unique_code_param else ''
        uc_parse_rs = GoodsBlock.parse_unique_code(unique_code)
        if uc_parse_rs is None:
            raise forms.ValidationError('产品唯一码格式不正确')
        self.cleaned_data['gb'] = uc_parse_rs
        print(self.cleaned_data)

    def clean_store_unit(self):
        print(self.data)
        unique_code_url = self.data['unique_code']
        rs = urlparse(unique_code_url)
        params = parse_qs(rs.query, True)
        unique_code_param = params.get('unique_code', None)
        unique_code = unique_code_param[0] if unique_code_param else ''
        store_unit_repo_no = self.data['store_unit_repo_no']
        uc_parse_rs = GoodsBlock.parse_unique_code(unique_code)
        surn_prase_rs = StoreUnit.parse_store_unit_repo_no(store_unit_repo_no)
        if uc_parse_rs is None:
            raise forms.ValidationError('产品唯一码格式不正确')
        if surn_prase_rs is None:
            raise forms.ValidationError('库存码格式不正确')
        self.cleaned_data['gb'] = uc_parse_rs
        self.cleaned_data['store_unit'] = surn_prase_rs
        print(self.cleaned_data)


    # store_unit_repo_no = forms.CharField(label="存储编码", required=False)
    # out_repo = forms.BooleanField(label='是否出库', required=False)